package com.aaron.kjviewutils.stock;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aaron.kjviewutils.JSONUtil;
import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.RecycleViewDividerForList;
import com.aaron.kjviewutils.eventBus.BundleEventMsg;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Aaron on 2017/3/1.
 */

public class StockFragment extends Fragment {

    @Bind(R.id.stocks)
    RecyclerView stocks;

    private StockActivity rootAty;
    private View rootView;
    private List<StockBean.StockInner> datas;
    private StockInnerAdapter adapter;
    private String data;
    private String continentType;
    private String countryName;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        rootAty = (StockActivity) getActivity();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frgment_stock, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData()
    {
        datas = new ArrayList<StockBean.StockInner>();
        adapter = new StockInnerAdapter(rootAty, datas);
        View header = LayoutInflater.from(rootAty).inflate(R.layout.frgment_head, null);
        TextView headTag = (TextView) header.findViewById(R.id.headTag);
        adapter.setHeaderView(header);
        Bundle bundle = getArguments();
        if (bundle != null)
        {
            data = bundle.getString("stock");
            continentType = bundle.getString("continentType");
            countryName = bundle.getString("countryName");
            if(!TextUtils.isEmpty(data))
            {
                JsonParser parser = new JsonParser();
                JsonArray json01 = parser.parse(data).getAsJsonArray();
                int size = json01.size();
                if(size>0)
                {
                    headTag.setText("股指："+size+"条");
                    datas.clear();
                    Iterator<JsonElement> iterator = json01.iterator();
                    List<StockBean.StockInner> stockInners = new ArrayList<StockBean.StockInner>();
                    while (iterator.hasNext())
                    {
                        JsonElement jsonElement = iterator.next();
                        if(jsonElement.isJsonObject())
                        {
                            StockBean stockBean = new StockBean();
                            JSONUtil<StockBean.StockInner> jsonUtil = new JSONUtil<StockBean.StockInner>();
                            StockBean.StockInner stock = stockBean.new StockInner();
                            stock = jsonUtil.toBean(jsonElement.toString(), stock);
                            stockInners.add(stock);
                        }
                    }
                    datas.addAll(stockInners);
                    adapter.notifyDataSetChanged();
                }
                else
                {
                    headTag.setText("未获取到股指数据");
                    adapter.notifyDataSetChanged();
                }
            }
            else
            {
                headTag.setText("未获取到股指数据");
                datas.clear();
                adapter.notifyDataSetChanged();
            }
            stocks.setLayoutManager(new LinearLayoutManager(rootAty));
            stocks.setAdapter(adapter);
            adapter.setmOnItemClickListener(new StockInnerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    //点击事件
                    StockBean.StockInner stock = null;
                    if(adapter.isHasHead())
                    {
                        stock = datas.get(position-1);
                    }
                    else
                    {
                        stock = datas.get(position);
                    }
                    Bundle bundle2 = new Bundle();
                    bundle2.putString("continentType", continentType);
                    bundle2.putString("countryName", countryName);
                    bundle2.putString("code", stock.code);
                    rootAty.eventBus.post(new BundleEventMsg(BundleEventMsg.EventBusType.STOCK_SUBMEU_UI_DETAIL, bundle2));
                }
            });
            stocks.addItemDecoration(new RecycleViewDividerForList(rootAty, LinearLayout.HORIZONTAL));
            stocks.setItemAnimator(new DefaultItemAnimator()); // 默认动画
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        rootAty = null;
    }
}
